package org.jeecg.modules.online.cgform.model;


import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.online.cgform.util.CgformUtil;

/**
 * sql 查询 order by 信息
 */
public class SqlOrder {


    /**
     * 排序规则 升序
     */
    public static final String ASC = "asc";

    /**
     * 排序规则 倒序
     */
    public static final String DESC = "desc";

    /**
     * sql排序关键字 ORDER_BY
     */
    public static final String ORDER_BY = " ORDER BY ";

    /**
     * 默认的排序字段
     */
    public static final String DEFAULT_ORDER_COLUMN = "ID";

    /**
     * 排序列
     */
    private String column;

    /**
     * 排序规则
     */
    private String rule;

    /**
     * 别名 带. 【联合查询新逻辑】
     */
    private String alias;

    /**
     * 创建默认的排序字段
     * @return
     */
    public static SqlOrder createDefaultOrder(){
        return createDefaultOrder("");
    }

    /**
     * 创建默认的排序字段 带别名的
     * @param alias 带.  如a.
     * @return
     */
    public static SqlOrder createDefaultOrder(String alias){
        SqlOrder sqlOrder = new SqlOrder(DEFAULT_ORDER_COLUMN);
        sqlOrder.setAlias(alias);
        return sqlOrder;
    }

    /**
     * 获取对象实际的sql语句
     * @return
     */
    public String getRealSql(){
        String sql = this.alias+oConvertUtils.camelToUnderline(column);
        if(ASC.equals(rule)) {
            sql+=" "+ASC;
        }else {
            sql+=" "+DESC;
        }
        return sql;
    }

    public SqlOrder() {
    }

    /**
     * 构造器
     * @param column
     * @param rule
     */
    public SqlOrder(String column, String rule) {
        this.column = column;
        this.rule = rule;
        this.alias = "";
    }

    /**
     * 构造器 默认排序 倒序
     * @param column
     */
    public SqlOrder(String column) {
        this.rule = DESC;
        this.column = column;
        this.alias = "";
    }

    public String getColumn() {
        return column;
    }

    public void setColumn(String column) {
        this.column = column;
    }

    public String getRule() {
        return rule;
    }

    public void setRule(String rule) {
        this.rule = rule;
    }

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }
}
